﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using MAntLibrary.Utility;
using MAntLibrary.NDEntities;

namespace MAntLibrary.suppliers
{
    public class Ratan
    {
        public static Result ReadXML(FileInfo file, ref List<ND_Commodity> ListOfCommodities, ref List<ND_Category> ListOfCategories)
        {
            Result result = new Result();
            ND_Commodity zbozi;

            XmlDocument doc = new XmlDocument();
            XmlNode root = null;
            try { doc.Load(file.FullName); }
            catch (FileNotFoundException FNFE)
            {
                Console.WriteLine("File Not Found");
                result.ResultCode = ResultCode.ERR;
                return result;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                result.ResultCode = ResultCode.ERR;
                return result;
            }

            // pruchod vnorenymi uzly korenoveho elementu
            root = doc.DocumentElement;

            foreach (XmlNode lNode in root.ChildNodes)
            {
                zbozi = new ND_Commodity();
                zbozi.PictureList = new List<ND_Picture>();
                zbozi.AttributteList = new List<ND_Attribute>();

                if (lNode.Name.Equals("product"))
                {
                    //ted jsem v uzlu SHOPITEM a zpracuju jednotlive hodnoty
                    XmlNodeList deti = lNode.ChildNodes;

                    foreach (XmlAttribute s in lNode.Attributes)
                    {
                        if (s.Name.Equals("avail")) { zbozi.Dostupnost = s.Value; }
                        else if (s.Name.Equals("priceOrigNet")) { zbozi.CenaBezDPH = s.Value; }
                        else if (s.Name.Equals("priceOrig")) { zbozi.CenaSDPH = s.Value; }
                    }

                    foreach (XmlNode d in deti)
                    {
                        if (d.Name.Equals("category"))
                        {
                            zbozi.Kategorie = d.InnerText;
                            continue;
                        }
                        if (d.Name.Equals("name")) { zbozi.JmenoVyrobku = d.InnerText; continue; }



                        if (d.Name.Equals("images"))
                        {
                            foreach (XmlNode x in d)
                            {
                                XmlAttributeCollection atr = x.Attributes;
                                ND_Picture P = new ND_Picture();
                                Uri uri;
                                foreach (XmlAttribute a in atr)
                                {
                                    if (a.Name.Equals("url")) { P.preview = a.Value; }
                                    else if (a.Name.Equals("origurl"))
                                    {
                                        P.fullImage = a.Value;
                                        uri = new Uri(a.Value);
                                        P.filename = System.IO.Path.GetFileName(uri.LocalPath);
                                    }
                                }
                                zbozi.PictureList.Add(P);
                            }
                            continue;
                        }




                        if (d.Name.Equals("description"))
                        {
                            zbozi.Popis = d.InnerText;
                            continue;
                        }
                        if (d.Name.Equals("description_html")) { zbozi.KratkyPopis = d.InnerText; continue; }

                        if (d.Name.Equals("attributes"))
                        {
                            foreach (XmlNode nd in d.ChildNodes)
                            {
                                zbozi.AttributteList.Add(new ND_Attribute(nd.Attributes[0].Value, nd.InnerText));
                            }
                            continue;
                        }
                    }
                }
                ListOfCommodities.Add(zbozi);
            }

            Console.WriteLine(string.Format("Nacteno zaznamu: {0}", ListOfCommodities.Count));
            return result;
        }
    }
}
